package ea977;

//package com.mkyong.core;
//package inseredados;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author antonio
 */
public class criaXML_1 {
    public static File fileXML;
    
    public void criaXML_1(){
        //fileXML = new File("c:\\users/Igor/Documents/fileProjeto.xml");
    }
    public File setaArquivoXML(String Endereco){
        File fileXML1 = new File (Endereco);
        return fileXML1;
    }
    
            //o que será passado como parâmetro será é o conteúdo das tags
            public void novoXML(String fileXML1, String Content_resumo, String Content_link, String Content_data, String Content_keyword) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  		//plausivel de refatoracao
                File xml = new File (fileXML1);
                criaXML_1.fileXML = xml;
                
                // root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("ProjetoL");
		doc.appendChild(rootElement);
                
		// PL elements
		//Element TAG_PL = doc.createElement("PL");
		//rootElement.appendChild(TAG_PL);
                /*CASO SEJA NECESSARIO ADICIONAR UM ATRIBUTO*/
		// set attribute to staff element
		//Attr attr = doc.createAttribute("id");
		//attr.setValue("1");
		//staff.setAttributeNode(attr);
 
		// shorten way
		// staff.setAttribute("id", "1");
 
		// firstname elements
		//Element RESUMO = doc.createElement("RESUMO");
		//RESUMO.appendChild(doc.createTextNode(Content_resumo));
		//TAG_PL.appendChild(RESUMO);
 
		// lastname elements
		//Element KEYWORD = doc.createElement("KEYWORD");
		//KEYWORD.appendChild(doc.createTextNode(Content_keyword));
		//TAG_PL.appendChild(KEYWORD);
 
		// nickname elements
		//Element DATA = doc.createElement("DATA");
		//DATA.appendChild(doc.createTextNode(Content_data));
		//TAG_PL.appendChild(DATA);
 
		// salary elements
		//Element LINK = doc.createElement("LINK");
		//LINK.appendChild(doc.createTextNode(Content_link));
		//TAG_PL.appendChild(LINK);
                
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(criaXML_1.fileXML);
 
		// Output to console for testing
		 //StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
    }
    //o conteúdo das tags será passado como parâmetro para construir o XML.
            public void insereNovoNOXML(String Content_resumo, String Content_link, String Content_data, String Content_keyword) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException{
                //File fXmlFile = new File("c:\\users/Igor/Documents/filenew.xml");
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(criaXML_1.fileXML);
                doc.getDocumentElement().normalize();
                
                Element TAG_PL = doc.createElement("PL");
                
                Element root = doc.getDocumentElement();  
                root.appendChild(TAG_PL);
                
                		// PL elements
		//Element TAG_PL = doc.createElement("PL");
		//rootElement.appendChild(TAG_PL);
                /*CASO SEJA NECESSARIO ADICIONAR UM ATRIBUTO*/
		// set attribute to staff element
		//Attr attr = doc.createAttribute("id");
		//attr.setValue("1");
		//staff.setAttributeNode(attr);
 
		// shorten way
		// staff.setAttribute("id", "1");
 
		// firstname elements
		Element RESUMO = doc.createElement("RESUMO");
		RESUMO.appendChild(doc.createTextNode(Content_resumo));
		TAG_PL.appendChild(RESUMO);
 
		// lastname elements
		Element AUTORES = doc.createElement("AUTORES");
		AUTORES.appendChild(doc.createTextNode(Content_keyword));
		TAG_PL.appendChild(AUTORES);
 
		// nickname elements
		Element NUMERO = doc.createElement("NUMERO");
		NUMERO.appendChild(doc.createTextNode(Content_data));
		TAG_PL.appendChild(NUMERO);
 
		// salary elements
		Element LINK = doc.createElement("LINK");
		LINK.appendChild(doc.createTextNode(Content_link));
		TAG_PL.appendChild(LINK);
                
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(criaXML_1.fileXML);
 
		// Output to console for testing
		 //StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
		//rootElement.appendChild(TAG_PL);
            }
            
            
}
